#include "ScriptCompiler/ScriptFileContext.h"
#include "Containers/String.h"
#include "ScriptFileContext.h"
#include "ScriptCompiler/Token/Token.h"
#include "ScriptCompiler/CompilerContext.h"

CScriptFileContext::CScriptFileContext(std::weak_ptr<CCompilerContext> InCompiler ,const CString &CSFile,const SPath& InFilePath)
{
    FilePath=InFilePath;
    Compiler=InCompiler;
    FileContent=CSFile.GetUnicodes();
    LineStartIndexList.Add(0);

    if(FileContent.Num()==0  || FileContent[0]==U'\0' )
    {
        RaiseError(10001,U"Empty file",true);
        return;
    }

}

void CScriptFileContext::RaiseError(uint64_t ErrorCode, uint64_t LineIndex, uint64_t ColumnIndex, uint64_t CharNum, const CString &InErrorMessage, bool bIsWarning)
{
    Compiler.lock()->RaiseError(ErrorCode,FilePath,SScriptFilePosition(LineIndex,ColumnIndex,CharNum),InErrorMessage,bIsWarning);
}

void CScriptFileContext::RaiseError(uint64_t ErrorCode,const SScriptFilePosition &PositionInFile, const CString &InErrorMessage,bool bIsWarning)
{
    Compiler.lock()->RaiseError(ErrorCode,FilePath,PositionInFile,InErrorMessage,bIsWarning);
}
void CScriptFileContext::RaiseError(uint64_t ErrorCode,std::shared_ptr<CScriptToken> InToken, const CString &InErrorMessage,bool bIsWarning)
{
    Compiler.lock()->RaiseError(ErrorCode,FilePath,InToken,InErrorMessage,bIsWarning);
}
void CScriptFileContext::RaiseError(uint64_t ErrorCode,const CString &InErrorMessage,bool bIsWarning)
{
    Compiler.lock()->RaiseError(ErrorCode,FilePath,InErrorMessage,bIsWarning);
}

#include "Output/Console.h"

void CScriptFileContext::DebugPrintTokenList()
{
    for(auto & Token : TokenList)
    {
        WH::Print(Token->ToString()+U" ");
    }
}

uint32_t CScriptFileContext::GetLineCount()
{
    return LineStartIndexList.Num();
}

std::shared_ptr<CScriptToken> CScriptFileContext::GetFormerToken()
{
    if(CurrentTokenIndex==0)
    {
        return nullptr;
    }
    return TokenList[CurrentTokenIndex-1];
}

std::shared_ptr<CScriptToken> CScriptFileContext::GetCurrentToken()
{
    if(CurrentTokenIndex>=TokenList.Num())
    {
        return nullptr;
    }
    return TokenList[CurrentTokenIndex];
}

std::shared_ptr<CScriptToken> CScriptFileContext::GetNextToken()
{
    if(CurrentTokenIndex>=(TokenList.Num()-1))
    {
        return nullptr;
    }
    return TokenList[CurrentTokenIndex+1];
}

std::shared_ptr<CScriptToken> CScriptFileContext::GetNextNextToken()
{
    if(CurrentTokenIndex>=(TokenList.Num()-2))
    {
        return nullptr;
    }
    return TokenList[CurrentTokenIndex+2];
}

std::shared_ptr<CScriptToken> CScriptFileContext::GetToken(uint64_t Index)
{
    return TokenList[Index];
}
